#include "board.hpp"
#include <algorithm>

namespace Tetris {
Board::Board() {
    clear();
}

void Board::clear() {
    for (int y = 0; y < BOARD_HEIGHT; ++y) {
        for (int x = 0; x < BOARD_WIDTH; ++x) {
            m_grid[y][x] = BlockType::None;
        }
    }
}

bool Board::isCollision(const Piece& piece) const {
    for (const auto& p : piece.getAbsoluteCoords()) {
        if (p.x < 0 || p.x >= BOARD_WIDTH || p.y >= BOARD_HEIGHT) {
            return true;
        }
        if (p.y >=0 && m_grid[p.y][p.x] != BlockType::None) {
            return true;
        }
    }
    return false;
}

void Board::mergePiece(const Piece& piece) {
    int colorId = piece.getColorId();
    BlockType type = piece.getType();

    for (const auto& p : piece.getAbsoluteCoords()) {
        if (p.y >= 0 && p.y < BOARD_HEIGHT && p.x >= 0 && p.x < BOARD_WIDTH) {
            m_grid[p.y][p.x] = type;
        }
    }
}

int Board::clearLines() {
    int clearedCount = 0;
    for (int y = BOARD_HEIGHT - 1; y >= 0; --y) {
        bool lineFull = true;

        for (int x = 0; x < BOARD_WIDTH; ++x) {
            if (m_grid[y][x] == BlockType::None) {
                lineFull = false;
                break;
            }
        }

        if (lineFull) {
            ++clearedCount;
            for (int r = y; r > 0; --r) {
                for (int c = 0; c < BOARD_WIDTH; ++c) {
                    m_grid[r][c] = m_grid[r - 1][c];
                }
            }
            // 最顶行清空
            for (int  c = 0; c < BOARD_WIDTH; ++c) {
                m_grid[0][c] = BlockType::None;
            }
            ++y;
        }
    }
    return clearedCount;
}

void Board::draw(WINDOW* win, int offset_x, int offset_y) const {
    // 边框绘制
    wattron(win, COLOR_PAIR(COLOR_PAIR_BORDER_TEXT));
    box(win, 0, 0);

    // 绘制游戏板块内容
    for (int y = BOARD_HIDDEN_TOP; y < BOARD_HEIGHT; ++y) {
        for (int x = 0; x < BOARD_WIDTH; ++x) {
            BlockType blockType = m_grid[y][x];
            int colorId = COLOR_PAIR_EMPTY; // 默认是空方块

            switch(blockType) {
                case BlockType::I: colorId = COLOR_PAIR_I_BLOCK; break;
                case BlockType::O: colorId = COLOR_PAIR_O_BLOCK; break;
                case BlockType::T: colorId = COLOR_PAIR_T_BLOCK; break;
                case BlockType::S: colorId = COLOR_PAIR_S_BLOCK; break;
                case BlockType::Z: colorId = COLOR_PAIR_Z_BLOCK; break;
                case BlockType::J: colorId = COLOR_PAIR_J_BLOCK; break;
                case BlockType::L: colorId = COLOR_PAIR_L_BLOCK; break;
                default: colorId = COLOR_PAIR_EMPTY; break;
            }

            wattron(win, COLOR_PAIR(colorId));
            // 绘制在窗口的相对坐标(y - BOARD_HIDDEN_TOP + 1, x * 2 + 1)
            // +1 是因为边框占一行
            mvwprintw(win, offset_y + (y - BOARD_HIDDEN_TOP) + 1, offset_x + (x * 2) + 1, (blockType == BlockType::None) ? "  " : "[]");
            wattroff(win, COLOR_PAIR(colorId));
        }
    }
}

BlockType Board::getBlockAt(int x, int y) const {
    if (x >= 0 && x < BOARD_WIDTH && y >= 0 && y < BOARD_HEIGHT) {
        return m_grid[y][x];
    }
    return BlockType::None;
}

} // namespace Tetris

